package sadi.server;

import sadi.util.CardsCollection;

public class Dealer extends BlackJackers
{
	//Instance Variables
	private String cards[] = new String[MAX_CARDS];
	private int deckTotal, totalCards, showCard;
	
	public Dealer(int status)
	{
		super(DEALER_USERNAME, status);
		deckTotal = totalCards = 0;
	}
	
	
	
	//Get A Dealer Card To Show To Clients
	public String showInitCard(CardsCollection cc, boolean isGameStart)
	{		
		if(isGameStart)
		{
			//Always Show Dealer's Best Card At Game Starting Point
			showCard = (cc.getCardValue(cards[0]) > cc.getCardValue(cards[1]))? 0 : 1; 		
			return cards[showCard];
		}
		else
			return (showCard == 0)? cards[1] : cards[0];
	}
	
	
	
	/*************** INHERITED METHODS ***************/
	
	//Reset Counters & Variables Before Starting A New Trial
	public void resetGame()
	{		
		deckTotal = 0;
		totalCards = 0;
		setBestChance(0);
		
		for(int i=0; i<MAX_CARDS; i++)
			cards[i] = null;
	}
	
	//Get Current Card Deck
	public String[] getCards()
	{
		return cards;
	}
	
	//Set Initial State of Trial: Drawn 2 Random Cards
	public void initGame(CardsCollection cc, String card1, String card2)
	{
		cards[0] = card1;
		cards[1] = card2;
		totalCards = 2;
		deckTotal = cc.countTotal(cards);
	}
	
	//Signal That Game Is Starting
	//Send 3 Initial Info: Dealer's 1st Card & Player's Randomly Drawn Cards
	public void startGame(String dealerCard)
	{
		setStatus(INGAME_WAIT);
	}
	
	//Signal For Next Move
	public int nextMove()
	{
		setStatus(INGAME_MYTURN);
		return (deckTotal < 17)? ACT_HIT : ACT_STAND;
	}
	
	//Check Whether Has Next Move To Make
	public boolean hasNextMove()
	{
		if(getStatus() == INGAME_WAIT)
		{
			if(deckTotal == MAX_POINTS)
			{
				//Initial 2 Cards Brings Blackjack
				setStatus(INGAME_STAND);
				setBestChance(deckTotal);				
				return false;
			}
			else
				return true;
		}
		else if(getStatus() == INGAME_CONTINUE)
			return true;		
		else
			return false;
	}
	
	//Hit Action Process
	public void hit(CardsCollection cc, String card)
	{
		int result;
		
		if(totalCards < MAX_CARDS)
		{
			//Update Counters
			cards[totalCards] = card;
			totalCards = totalCards + 1;
			deckTotal = cc.countTotal(cards);
			
			//Check New Status
			if(deckTotal > MAX_POINTS)
				result = INGAME_BURST;
			else if((totalCards == MAX_CARDS) || (deckTotal == MAX_POINTS))
			{
				result = INGAME_STAND;
				setBestChance(deckTotal);
			}
			else
				result = INGAME_CONTINUE;
			
			setStatus(result);
		}
	}
	
	//Stand Action Process
	public void stand()
	{
		setStatus(INGAME_STAND);
		setBestChance(deckTotal);
	}
	
	//Split Action Process
	public void split(CardsCollection cc)
	{		
		//Left empty intentionally: Dealer can't split!
	}
	
	//Get Counting Total of Current Deck (Which May Be Burst Score)
	public int getCurrentChance()
	{
		return deckTotal;
	}
}
